/*
 * Copyright 2013-2019 Xia Jun(3979434@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ***************************************************************************************
 *                                                                                     *
 *                        Website : http://www.farsunset.com                           *
 *                                                                                     *
 ***************************************************************************************
 */
package com.workcell.workdata.im.mvc.controller.moment;

import com.workcell.workdata.im.annotation.UID;
import com.workcell.workdata.im.entity.MomentRule;
import com.workcell.workdata.im.model.response.ResponseEntity;
import com.workcell.workdata.im.model.vo.MomentRuleBatchVO;
import com.workcell.workdata.im.model.vo.MomentRuleVO;
import com.workcell.workdata.im.service.MomentRuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;

@RestController
@RequestMapping("/moment")
@Api(produces = "application/json", tags = "朋友圈权限接口" )
@Validated
public class MomentRuleController {

	@Resource
	private MomentRuleService momentRuleService;


	@ApiOperation(httpMethod = "POST", value = "设置朋友圈权限")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "targetId", value = "用户ID", paramType = "path", dataTypeClass = Long.class,example = "0"),
			@ApiImplicitParam(name = "type", value = "类型(1:我不看TA的 0:不给TA看我的)", paramType = "path", dataTypeClass = Integer.class,example = "0"),
	})
	@PostMapping(value = "/rule/{targetId}/{type}")
	public ResponseEntity<Void> save(@PathVariable Long targetId, @PathVariable byte type, @ApiIgnore @UID Long uid) {
		MomentRule rule = new MomentRule();
		rule.setTargetId(targetId);
		rule.setType(type);
		rule.setUid(uid);
		momentRuleService.save(rule);
		return ResponseEntity.ok();
	}

	@ApiOperation(httpMethod = "DELETE", value = "解除朋友圈权限")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "targetId", value = "用户ID", paramType = "path", dataTypeClass = Long.class,example = "0"),
			@ApiImplicitParam(name = "type", value = "类型(1:我不看TA的 0:不给TA看我的)", paramType = "path", dataTypeClass = Integer.class,example = "0"),
	})
	@DeleteMapping(value = "/rule/{targetId}/{type}")
	public ResponseEntity<Void> delete(@PathVariable Long targetId, @PathVariable byte type, @ApiIgnore @UID Long uid) {
		MomentRule rule = new MomentRule();
		rule.setTargetId(targetId);
		rule.setType(type);
		rule.setUid(uid);
		momentRuleService.delete(rule);
		return ResponseEntity.ok();
	}


	@ApiOperation(httpMethod = "GET", value = "获取我限制的好友列表")
	@GetMapping(value = "/rule/list")
	public ResponseEntity<MomentRuleBatchVO> findIgnoredList(@ApiIgnore @UID Long uid) {
		MomentRuleBatchVO vo = new MomentRuleBatchVO();
		vo.setIgnoredList(momentRuleService.findMeIgnoreList(uid));
		vo.setBlackedList(momentRuleService.findMeBlockedList(uid));
		return ResponseEntity.ok(vo);
	}

	@ApiOperation(httpMethod = "GET", value = "获取好友的朋友圈权限")
	@GetMapping(value = "/rule/{targetId}")
	public ResponseEntity<MomentRuleVO> findOne(@ApiIgnore @UID Long uid, @PathVariable long targetId) {
		MomentRuleVO vo = new MomentRuleVO();
		vo.setBlacked(momentRuleService.isBlacked(uid,targetId));
		vo.setIgnored(momentRuleService.isIgnored(uid,targetId));
		return ResponseEntity.ok(vo);
	}
}
